﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Aricie.DNN.Services.Url.FriendlyUrl;
using System.Web.Behaviors;
using DotNetNuke.Entities.Tabs;
using System.Web.Moles;
using Aricie.DNN.Test.TestFramework.Services.Mocks;
using Aricie.DNN.Services.Url.FriendlyUrl.Moles;
using DotNetNuke.Framework;
using Aricie.FriendlierUrlProvider.Test.Mocks;

namespace Aricie.FriendlierUrlProvider.Test.Components.Entities.Groups
{
    [TestClass]
    public class FriendlierUrlRuleTestGetRewrite
    {
        [TestMethod]
        [HostType("Moles")]
        public void TestGetRewriteHomePage()
        {
            HttpContextMocks.Mock();
            NukeHelperMocks.Mock();
            FupMocks.Mock();
            SettingsControllerMocks.Mock();
            DotNetNukeMocks.Mock();

            UrlRewriteInput rewriteInput = new UrlRewriteInput(FupMocks.FupContext, DotNetNukeMocks.Home, new Uri("http://www.test.com/Default.aspx"), "www.test.com");
            UrlRewriteContext rewriteContext = new UrlRewriteContext(rewriteInput);
            FriendlierUrlRule urlRule = GetRule("${BASE_REPLACERULE_ROOT}${BASE_REPLACERULE_END}");

            VariableRewriteResult result = urlRule.GetRewrite(rewriteContext);

            Assert.AreEqual<string>("http://www.test.com/fr/home.aspx", result.Rewrite);
        }

        [TestMethod]
        [HostType("Moles")]
        public void TestGetRewrite()
        {
            HttpContextMocks.Mock();
            NukeHelperMocks.Mock();
            FupMocks.Mock();
            SettingsControllerMocks.Mock();
            DotNetNukeMocks.Mock();

            UrlRewriteInput rewriteInput = new UrlRewriteInput(FupMocks.FupContext, DotNetNukeMocks.TestTab, new Uri("http://localhost/aricie/Default.aspx?tabid=100&param=1"), "localhost/aricie");
            UrlRewriteContext rewriteContext = new UrlRewriteContext(rewriteInput);
            FriendlierUrlRule urlRule = GetRule("[${Scheme}][${UserInfo}][${RootPath}][/${Lang}][/${TabPath,SubPath}][/${TabName}][/${ModuleTitle}][/${ControlKey}][/${Params,SubPath}][.${Extension}][?${QueryString,QueryStringValue}][#${Fragment,QueryStringFlag}]");

            VariableRewriteResult result = urlRule.GetRewrite(rewriteContext);

            Assert.AreEqual<string>("http://localhost/aricie/fr/parent/test/param/1.aspx", result.Rewrite);

        }

        [TestMethod]
        [HostType("Moles")]
        public void TestGetRewriteWithoutParams()
        {
            HttpContextMocks.Mock();
            NukeHelperMocks.Mock();
            FupMocks.Mock();
            SettingsControllerMocks.Mock();
            DotNetNukeMocks.Mock();

            UrlRewriteInput rewriteInput = new UrlRewriteInput(FupMocks.FupContext, DotNetNukeMocks.TestTab, new Uri("http://localhost/aricie/Default.aspx?tabid=100&param=1"), "localhost/aricie");
            UrlRewriteContext rewriteContext = new UrlRewriteContext(rewriteInput);
            FriendlierUrlRule urlRule = GetRule("[${Scheme}][${UserInfo}][${RootPath}][/${Lang}][/${TabPath,SubPath}][/${TabName}][/${ModuleTitle}][/${ControlKey}][.${Extension}][?${QueryString,QueryStringValue}][#${Fragment,QueryStringFlag}]");

            VariableRewriteResult result = urlRule.GetRewrite(rewriteContext);

            Assert.AreEqual<string>("http://localhost/aricie/fr/parent/test.aspx?param=1", result.Rewrite);

        }

        [TestMethod]
        [HostType("Moles")]
        public void TestGetRewriteAliases()
        {
            HttpContextMocks.Mock();
            NukeHelperMocks.Mock();
            FupMocks.Mock();
            SettingsControllerMocks.Mock();
            DotNetNukeMocks.Mock();

            UrlRewriteInput rewriteInput = new UrlRewriteInput(FupMocks.FupContext, DotNetNukeMocks.TestTab, new Uri("http://localhost/aricie/Default.aspx?tabid=100&param=1"), "localhost/aricie");
            UrlRewriteContext rewriteContext = new UrlRewriteContext(rewriteInput);
            FriendlierUrlRule urlRule = GetRule("${BASE_REPLACERULE_ROOT}${BASE_REPLACERULE_END}");

            VariableRewriteResult result = urlRule.GetRewrite(rewriteContext);

            Assert.AreEqual<string>("http://localhost/aricie/fr/parent/test/param/1.aspx", result.Rewrite);

        }

        private FriendlierUrlRule GetRule(string rule)
        {
            FriendlierUrlStrategy strategy = new FriendlierUrlStrategy();

            strategy.StrategyProviders.Add(FupMocks.StandardUrlRewriterProvider);
            
            //ne pas supprimmer
            var tmp = strategy.GroupRewrites;

            strategy.RewriteRule = rule;


            FriendlierUrlRule urlRule = FriendlierUrlRule.FromFormerStrategy(strategy, FupMocks.FupContext.FriendlierUrlPortalSettings);

            return urlRule;
        }
    }
}
